package com.atguigu.myssm.filters;


import com.atguigu.myssm.basedao.ConnUtils;
import com.atguigu.myssm.trans.TransactionManager;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.SQLException;

@WebFilter("*.do")
public class OpenSessionViewFilter implements Filter {

    //private TransactionManager transactionManager;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException{
                try{
                    TransactionManager.beginTrans();
                    System.out.println("beginsTrans");
                    filterChain.doFilter(servletRequest,servletResponse);
                    TransactionManager.commitTrans();
                    System.out.println("commitTrans");
                }catch (Exception e){
                    e.printStackTrace();
                    try {
                        TransactionManager.rollbackTrans();
                        System.out.println("rollbackTrans");
                    } catch (SQLException ex) {
                       ex.printStackTrace();
                    }
                }
    }

    @Override
    public void destroy() {

    }
}
